//
//  HomeChannelDetailCollectionViewCell.m
//  TestApplication
//
//  Created by Hanlingyu on 16/9/7.
//  Copyright © 2016年 ltq. All rights reserved.
//

#import "HomeChannelDetailCollectionViewCell.h"
#import "TAMacro.h"
#import "Masonry.h"
#import "SelfSizeTool.h"
#import "TextCollectionViewCell.h"

#import "HomeChannelDetailModel.h"
#import "HomeChannelDetailContent.h"
#import "HomeChannelDetailImg.h"
#import "HomeChannelDetailTag.h"
#import "HomeChanneldetailCommentModel.h"

#import "UIButton+WebCache.h"
#import "UIImageView+WebCache.h"
//#import "NewActionContent.h"
//#import "NewActionTagModel.h"
//#import "NewActionOfCommentModel.h"
//#import "NewActionImg.h"
#import "UIView+Frame.h"

#import "DetailDescTableViewCell.h"
#import "DetailTagsTableViewCell.h"
#import "DetailCommentTableViewCell.h"
#import "LikedPersonTableViewCell.h"
#import "NoCommentDetailCommentTableViewCell.h"
@interface HomeChannelDetailCollectionViewCell ()
<
    UITableViewDelegate,
    UITableViewDataSource,
    UIScrollViewDelegate
>
@property (nonatomic, strong) UIScrollView *homeChannelDetailScrollView;
@property (nonatomic, strong) UITableView *homeChannelDetailTableView;
@property (nonatomic, strong) UIView *headerViewOfhomeChannelDetailTableView;
@property (nonatomic, strong) UIButton *headerImageButton;
@property (nonatomic, strong) UILabel *nameLabel;
@property (nonatomic, strong) UIImageView *vipImageView;
@property (nonatomic, strong) UIScrollView *imgScrollView;
@property (nonatomic, strong) UILabel *pageLabel;
@property (nonatomic, strong) UILabel *toolBarView;


@property (nonatomic, assign) CGFloat heigthOfDetailDescTableViewCell;
@property (nonatomic, assign) CGFloat heigthOfDetailTagsTableViewCell;
@property (nonatomic, assign) CGFloat heigthOfLikedPersonTableViewCell;
@property (nonatomic, assign) CGFloat heigthOfNoCommentDetailCommentTableViewCell;
@property (nonatomic, assign) CGFloat heigthOfDetailCommentTableViewCell;


@property (nonatomic, assign) CGFloat oldConetentInSet_Y;
@property (nonatomic, assign) CGFloat currentConetentInSet_Y;

@end


@implementation HomeChannelDetailCollectionViewCell


- (void)setHomeChannelDetailModel:(HomeChannelDetailContent *)homeChannelDetailModel {
    
    _homeChannelDetailModel = homeChannelDetailModel;

    _nameLabel.text = _homeChannelDetailModel.uname;
    //    NSLog(@"%@", _homeChannelDetailTableView);
    
    [_headerImageButton sd_setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"http://qn.zheli.io/%@", _homeChannelDetailModel.avatar]] forState:UIControlStateNormal];
    _headerImageButton.imageView.contentMode = UIViewContentModeScaleAspectFill;
    _headerImageButton.imageView.layer.masksToBounds = YES;
    _imgScrollView.contentSize = CGSizeMake(SCREEN_WIDTH * _homeChannelDetailModel.img.count, 200);
    for (NSInteger i = 0; i < _homeChannelDetailModel.img.count; i++) {
        HomeChannelDetailImg *img = _homeChannelDetailModel.img[i];
        UIImageView *imgView = [[UIImageView alloc] initWithFrame:CGRectMake(SCREEN_WIDTH * i, 0, SCREEN_WIDTH, SCREEN_WIDTH / img.w * img.h)];
        [imgView sd_setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"http://qn.zheli.io/%@", img.f]]];
//        NSLog(@"http://qn.zheli.io/%@", img.f);
//        NSLog(@"w:%ld, h:%ld", img.w, img.h);
        [_imgScrollView addSubview:imgView];
    }
    _imgScrollView.frame = CGRectMake(0, 55, SCREEN_WIDTH, SCREEN_WIDTH / _homeChannelDetailModel.img[0].w * _homeChannelDetailModel.img[0].h);
    
//    _homeChannelDetailScrollView.contentSize = CGSizeMake(SCREEN_WIDTH, SCREEN_HEIGHT);
    
    _headerViewOfhomeChannelDetailTableView.ltq_height = SCREEN_WIDTH / _homeChannelDetailModel.img[0].w * _homeChannelDetailModel.img[0].h + 55;
    [_homeChannelDetailTableView beginUpdates];
    _homeChannelDetailTableView.tableHeaderView = _headerViewOfhomeChannelDetailTableView;
    [_homeChannelDetailTableView endUpdates];
    
    CGFloat firstCellHeight = 0;
    if ([_homeChannelDetailModel.desc isEqualToString:@""]) {
        firstCellHeight = 0;
    } else {
        CGFloat height = [SelfSizeTool selfSizeHeightWithString:_homeChannelDetailModel.desc width:SCREEN_WIDTH - 40 font:[UIFont systemFontOfSize:14]];
        firstCellHeight = height + 25;
    }
//    NSLog(@"firstCellHeiht: %lf", firstCellHeight);
    
    CGFloat secondCellHeight = 0;
    if (_homeChannelDetailModel.tag.count != 0) {
        NSMutableArray *tagNameArray = [NSMutableArray array];
        for (NSInteger i = 0; i < _homeChannelDetailModel.tag.count; i++) {
            [tagNameArray addObject:_homeChannelDetailModel.tag[i].content];
        }
        [tagNameArray insertObject:@"#" atIndex:0];
        NSString *tagString = [tagNameArray componentsJoinedByString:@" "];
        CGFloat height = [SelfSizeTool selfSizeHeightWithString:tagString width:SCREEN_WIDTH - 40 font:[UIFont systemFontOfSize:13]];
        secondCellHeight = height + 20;
        
    } else {
        secondCellHeight = 0;
    }
//    NSLog(@"secondCellHeight: %lf", secondCellHeight);

    
    CGFloat thirdCellHeight = 0;
    if (_homeChannelDetailModel.likeNum == 0) {
        
    } else if (_homeChannelDetailModel.likeNum <= 8) {
        CGFloat height = (SCREEN_WIDTH - 60) / 8 + 20;
        thirdCellHeight = height;
        
    } else  {
        CGFloat height = (SCREEN_WIDTH - 60) / 4 + 20;
        thirdCellHeight = height;
    }
//    NSLog(@"thirdCellHeight: %lf", thirdCellHeight);

    
    CGFloat fourthCellHeight = 0;
    if (_homeChannelDetailModel.comment.count == 0) {
        NSString *string = @"写一条评论, 抢个沙发吧";
        CGFloat height = [SelfSizeTool selfSizeHeightWithString:string width:SCREEN_WIDTH - 100 font:[UIFont systemFontOfSize:13]];
        fourthCellHeight = height + 35;
        
    } else {
        CGFloat cellHeight = 0;
        for (HomeChanneldetailCommentModel *commentModel in _homeChannelDetailModel.comment) {
            
            if (commentModel.replyName) {
                NSString *string = [NSString stringWithFormat:@"回复 %@: %@", commentModel.replyName, commentModel.content];
                CGFloat height = [SelfSizeTool selfSizeHeightWithString:string width:SCREEN_WIDTH - 100 font:[UIFont systemFontOfSize:13]];
                cellHeight += (height + 35);
                //                return height + 35;
            } else {
                NSString *string = commentModel.content;
                CGFloat height = [SelfSizeTool selfSizeHeightWithString:string width:SCREEN_WIDTH - 100 font:[UIFont systemFontOfSize:13]];
                //                return height + 35;
                cellHeight += (height + 35);
            }
        }
        fourthCellHeight = cellHeight;
    }
//    NSLog(@"fourthCellHeight: %lf", fourthCellHeight);

    
//    NSLog(@"%lf", firstCellHeight + secondCellHeight + thirdCellHeight + fourthCellHeight + _headerViewOfhomeChannelDetailTableView.bounds.size.height);
    
    _homeChannelDetailScrollView.frame = CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT - 64-49);

    _homeChannelDetailTableView.frame = CGRectMake(0, 0, SCREEN_WIDTH, firstCellHeight + secondCellHeight + thirdCellHeight + fourthCellHeight + _headerViewOfhomeChannelDetailTableView.bounds.size.height);
    if ((firstCellHeight + secondCellHeight + thirdCellHeight + fourthCellHeight + _headerViewOfhomeChannelDetailTableView.bounds.size.height) >= SCREEN_HEIGHT - 64 - 49) {
        _homeChannelDetailScrollView.contentSize = CGSizeMake(SCREEN_WIDTH, firstCellHeight + secondCellHeight + thirdCellHeight + fourthCellHeight + _headerViewOfhomeChannelDetailTableView.bounds.size.height);

//        _homeChannelDetailTableView.scrollEnabled = YES;

    } else {
        _homeChannelDetailScrollView.contentSize = CGSizeMake(SCREEN_WIDTH, SCREEN_HEIGHT  - 49 - 64);
//        [self.contentView addSubview:_homeChannelDetailTableView];

        _homeChannelDetailTableView.scrollEnabled = NO;
    }
    
    
    
    
    [_homeChannelDetailTableView reloadData];
    
}


- (instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        self.contentView.backgroundColor = [UIColor colorWithWhite:0.917 alpha:1];
        [self createScrollView];
        [self createView];
        [self createHeaderImageButton];
        [self createNameLabel];
        [self createVipImageView];
        [self createImgScrollView];
        [self createToolBarView];
    }
    return self;
}

- (void)createScrollView {
    
    _homeChannelDetailScrollView = [[UIScrollView alloc] init];
    _homeChannelDetailScrollView.backgroundColor = [UIColor colorWithWhite:0.917 alpha:1];
    _homeChannelDetailScrollView.delegate = self;
    [self.contentView addSubview:_homeChannelDetailScrollView];
    
}

- (void)createView {
    _homeChannelDetailTableView = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStylePlain];
//    _homeChannelDetailTableView.backgroundColor = [UIColor whiteColor];
    _homeChannelDetailTableView.delegate = self;
    _homeChannelDetailTableView.dataSource = self;
//    [self.contentView addSubview:_homeChannelDetailTableView];
    [_homeChannelDetailScrollView addSubview:_homeChannelDetailTableView];
    [_homeChannelDetailTableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"reuse"];
    [_homeChannelDetailTableView registerNib:[UINib nibWithNibName:@"DetailDescTableViewCell" bundle:[NSBundle mainBundle]] forCellReuseIdentifier:@"DetailDescTableViewCellReuse"];
    [_homeChannelDetailTableView registerNib:[UINib nibWithNibName:@"DetailTagsTableViewCell" bundle:[NSBundle mainBundle]] forCellReuseIdentifier:@"DetailTagsTableViewCellReuse"];
    [_homeChannelDetailTableView registerClass:[DetailCommentTableViewCell class] forCellReuseIdentifier:@"DetailCommentTableViewCellReuse"];
    [_homeChannelDetailTableView registerClass:[LikedPersonTableViewCell class] forCellReuseIdentifier:@"LikedPersonTableViewCellReuse"];
    [_homeChannelDetailTableView registerNib:[UINib nibWithNibName:@"NoCommentDetailCommentTableViewCell" bundle:[NSBundle mainBundle]] forCellReuseIdentifier:@"NoCommentDetailCommentTableViewCellReuse"];
    
    _homeChannelDetailTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
//    _homeChannelDetailTableView.scrollEnabled = NO;
    
    [self createTableHeaderView];
}

- (void)createTableHeaderView {
    _headerViewOfhomeChannelDetailTableView = [[UIView alloc] initWithFrame:CGRectZero];
    _headerViewOfhomeChannelDetailTableView.backgroundColor = [UIColor whiteColor];
    _homeChannelDetailTableView.tableHeaderView = _headerViewOfhomeChannelDetailTableView;
}

- (void)createHeaderImageButton {
    _headerImageButton = [UIButton buttonWithType:UIButtonTypeCustom];
    _headerImageButton.backgroundColor = [UIColor yellowColor];
    [_headerViewOfhomeChannelDetailTableView addSubview:_headerImageButton];
}

- (void)createNameLabel {
    _nameLabel = [[UILabel alloc] init];
    [_headerViewOfhomeChannelDetailTableView addSubview:_nameLabel];
    _nameLabel.font = [UIFont systemFontOfSize:14];
}
- (void)createVipImageView {
    _vipImageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"vipComment"]];
    [_headerViewOfhomeChannelDetailTableView addSubview:_vipImageView];
}

- (void)createImgScrollView {
    
    _imgScrollView = [[UIScrollView alloc] initWithFrame:CGRectZero];
    _imgScrollView.backgroundColor = [UIColor cyanColor];
    _imgScrollView.pagingEnabled = YES;
    [_headerViewOfhomeChannelDetailTableView addSubview:_imgScrollView];
    
}

- (void)createToolBarView {
    
    _toolBarView = [[UILabel alloc] init];
    _toolBarView.backgroundColor = [UIColor redColor];
    _toolBarView.text = @"太难了";
    [self.contentView addSubview:_toolBarView];
    
    
}




#pragma mark - tableView
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return 4;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    if (indexPath.row == 0) {
        if ([_homeChannelDetailModel.desc isEqualToString:@""]) {
            self.heigthOfDetailDescTableViewCell = 0;
            return 0;
        }
        CGFloat height = [SelfSizeTool selfSizeHeightWithString:_homeChannelDetailModel.desc width:SCREEN_WIDTH - 40 font:[UIFont systemFontOfSize:14]];
        self.heigthOfDetailDescTableViewCell = height + 25;

        return _heigthOfDetailDescTableViewCell;
    } else if (indexPath.row == 1) {
        if (_homeChannelDetailModel.tag.count != 0) {
            NSMutableArray *tagNameArray = [NSMutableArray array];
            for (NSInteger i = 0; i < _homeChannelDetailModel.tag.count; i++) {
                [tagNameArray addObject:_homeChannelDetailModel.tag[i].content];
            }
            [tagNameArray insertObject:@"#" atIndex:0];
            NSString *tagString = [tagNameArray componentsJoinedByString:@" "];
            CGFloat height = [SelfSizeTool selfSizeHeightWithString:tagString width:SCREEN_WIDTH - 40 font:[UIFont systemFontOfSize:13]];
            self.heigthOfDetailTagsTableViewCell = height + 20;
            return _heigthOfDetailTagsTableViewCell;
        }
        self.heigthOfDetailTagsTableViewCell = 0;
        return _heigthOfDetailTagsTableViewCell;

    } else if (indexPath.row == 2) {
        if (_homeChannelDetailModel.likeNum == 0) {
            return 0;
        } else if (_homeChannelDetailModel.likeNum <= 8) {
            CGFloat height = (SCREEN_WIDTH - 60) / 8 + 20;
            self.heigthOfLikedPersonTableViewCell = height;
            return _heigthOfLikedPersonTableViewCell;
        } else  {
            CGFloat height = (SCREEN_WIDTH - 60) / 4 + 20;
            self.heigthOfLikedPersonTableViewCell = height;
            return _heigthOfLikedPersonTableViewCell;
        }
    } else  {
        if (_homeChannelDetailModel.comment.count == 0) {
            NSString *string = @"写一条评论, 抢个沙发吧";
            CGFloat height = [SelfSizeTool selfSizeHeightWithString:string width:SCREEN_WIDTH - 100 font:[UIFont systemFontOfSize:13]];
            self.heigthOfNoCommentDetailCommentTableViewCell = height + 35;
            return _heigthOfNoCommentDetailCommentTableViewCell;
        } else {
            CGFloat cellHeight = 0;
            for (HomeChanneldetailCommentModel *commentModel in _homeChannelDetailModel.comment) {
                
                if (commentModel.replyName) {
                    NSString *string = [NSString stringWithFormat:@"回复 %@: %@", commentModel.replyName, commentModel.content];
                    CGFloat height = [SelfSizeTool selfSizeHeightWithString:string width:SCREEN_WIDTH - 100 font:[UIFont systemFontOfSize:13]];
                    cellHeight += (height + 35);
                    //                return height + 35;
                } else {
                    NSString *string = commentModel.content;
                    CGFloat height = [SelfSizeTool selfSizeHeightWithString:string width:SCREEN_WIDTH - 100 font:[UIFont systemFontOfSize:13]];
                    //                return height + 35;
                    cellHeight += (height + 35);
                }
            }
            self.heigthOfDetailCommentTableViewCell = cellHeight;
            return _heigthOfDetailCommentTableViewCell;
        }
    }
    return 100;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    if (indexPath.row == 0) {
        DetailDescTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"DetailDescTableViewCellReuse"];
        cell.actionContent = _homeChannelDetailModel;
        return cell;
    } else if (indexPath.row == 1) {
        DetailTagsTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"DetailTagsTableViewCellReuse"];
        cell.actionContent = _homeChannelDetailModel;
        return cell;
    } else if (indexPath.row == 2) {
        
        LikedPersonTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"LikedPersonTableViewCellReuse"];
        cell.likeUsers = _homeChannelDetailModel.likeUsers;
        return cell;
        
        
    } else {
        
        if (_homeChannelDetailModel.commentNum == 0) {
            NoCommentDetailCommentTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"NoCommentDetailCommentTableViewCellReuse"];
            return cell;
        }
        DetailCommentTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"DetailCommentTableViewCellReuse"];
        cell.commentArray = _homeChannelDetailModel.comment;
        return cell;
    }
    
}

- (void)layoutSubviews {
    [super layoutSubviews];
    

    
    
    _toolBarView.frame = CGRectMake(0, SCREEN_HEIGHT - 64 - 49, SCREEN_WIDTH, 49);

    _toolBarView.backgroundColor = [UIColor redColor];
    
    
    
    
    [_headerImageButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.left.mas_equalTo(10);
        make.height.width.mas_equalTo(35);
    }];
    [_nameLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.mas_equalTo(self.headerImageButton.mas_centerY);
        make.left.mas_equalTo(self.headerImageButton.mas_right).mas_equalTo(10.0f);
    }];
    [_vipImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.height.width.mas_equalTo(12);
        make.left.mas_equalTo(self.headerImageButton.mas_right).mas_offset(-6);
        make.bottom.mas_equalTo(self.headerImageButton.mas_bottom).mas_offset(3);
    }];
//    _imgScrollView.frame = CGRectMake(0, 55, SCREEN_WIDTH, 200);
//    _imgScrollView.contentSize = CGSizeMake(SCREEN_WIDTH * 3, 200);
    

    
}



- (void)prepareForReuse {
    
    _homeChannelDetailScrollView.contentOffset = CGPointMake(0, 0);
    
}


// 开始拖拽的偏移量
- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView {
    
    if (scrollView == _homeChannelDetailTableView) {

        self.oldConetentInSet_Y = scrollView.contentOffset.y;
    }
    
}

- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
    
   
    
    self.currentConetentInSet_Y = scrollView.contentOffset.y;
    if (_oldConetentInSet_Y - _currentConetentInSet_Y > -4) {
//        NSLog(@"向下");
    } else {
//        NSLog(@"向上");
    }
}


- (void)awakeFromNib {
    // Initialization code
}
@end
